package com.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.IService;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.impl.AttrAttrgroupRelationServiceImpl;
import com.gulimall.product.service.impl.AttrGroupServiceImpl;
import com.gulimall.product.service.impl.AttrServiceImpl;
import com.gulimall.product.service.impl.CategoryServiceImpl;
import com.gulimall.product.vo.GroupWithAttrVo;
import org.springframework.web.bind.annotation.*;

import com.gulimall.product.entity.AttrGroupEntity;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 16:30:53
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupServiceImpl attrGroupServiceImpl;
    @Resource
    private CategoryServiceImpl categoryServiceImpl;
    @Resource
    private AttrServiceImpl attrServiceImpl;
    @Resource
    private AttrAttrgroupRelationServiceImpl relationServiceImpl;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
//    @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Integer catelogId){
        PageUtils page = attrGroupServiceImpl.queryPageById(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 查询关联数据
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
//    @RequiresPermissions("product:attrgroup:list")
    public R getRelationList(@PathVariable Long attrGroupId){
        List<AttrEntity> list = attrServiceImpl.queryRelationList(attrGroupId);

        return R.ok().put("data", list);
    }

    /**
     * 查询未关联数据
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
//    @RequiresPermissions("product:attrgroup:list")
    public R getNoRelationList(@PathVariable Long attrGroupId, @RequestParam Map<String,Object> params){
        PageUtils page = attrServiceImpl.queryNoRelationList(params, attrGroupId);

        return R.ok().put("page", page);
    }

    /**
     * 查询分类下所有分组以及各个分组关联的属性
     */
    @GetMapping("/{cateId}/withattr")
//    @RequiresPermissions("product:attrgroup:list")
    public R getGroupWithAttr(@PathVariable("cateId") Long cateId){
        List<GroupWithAttrVo> groupWithAttrVoList = attrServiceImpl.getGroupWithAttr(cateId);
        return R.ok().put("data", groupWithAttrVoList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
//    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupServiceImpl.getById(attrGroupId);
        Long[] fatherIds = categoryServiceImpl.getFatherIds(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(fatherIds);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
        attrGroupServiceImpl.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
        attrGroupServiceImpl.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
        attrGroupServiceImpl.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 新增关联关系
     */
    @PostMapping("/attr/relation")
//    @RequiresPermissions("product:attrgroup:delete")
    public R addRelationList(@RequestBody AttrAttrgroupRelationEntity[] attrRelationVoArray){
        relationServiceImpl.saveBatch(Arrays.asList(attrRelationVoArray));
        return R.ok();
    }

    /**
     * 移除关联关系
     */
    @PostMapping("/attr/relation/delete")
//    @RequiresPermissions("product:attrgroup:delete")
    public R removeRelationList(@RequestBody AttrAttrgroupRelationEntity[] attrRelationVoArray){
        relationServiceImpl.deleteList(Arrays.asList(attrRelationVoArray));
        return R.ok();
    }

}
